package battleFieldModel;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import worldModel.Item;

/**
 * @author Chris Ray
 * Created on 4:31:34 AM Oct 30, 2011
 *
 */

/**
 * @author Chris Ray Created on 4:31:34 AM Oct 30, 2011
 */
public class TradeModel {
	
	private List<Item> navigatorList;
	private List<Item> givingList;
	private List<Item> receivingList;
	private ServerSocket server;
	private Socket peer;
	private boolean connected = false;


	/**
	 * 
	 * @author Chris Ray Created on 8:17:15 PM Oct 31, 2011
	 * 
	 */
	public TradeModel() {
		navigatorList = new ArrayList<Item>();
		
		givingList = new ArrayList<Item>();
		receivingList = new ArrayList<Item>();
		server = null;
		peer = null;

	}

	/**
	 * @return the navigatorList
	 * @author Chris Ray Created on 8:05:40 PM Oct 31, 2011
	 */
	public List<Item> getNavigatorList() {
		return navigatorList;
	}



	/**
	 * @author Ryan Robinson Created on 4:13:49 PM Nov 3, 2011
	 * 
	 */
	public void makeServer() {

		try {
			server = new ServerSocket(4000);
			server.setSoTimeout(30000);
			System.out
					.println("Server > Now Listening for Connections on Port: 4000");
			peer = server.accept();
			connected = true;
		} catch (IOException ioe) {
//			ioe.printStackTrace();
			connected = false;
			System.out.print("no connection. try again.");

			// On error, clean up the server socket if possible
			if (server != null)
				try {
					server.close();
				} catch (IOException ioe2) {
					ioe.printStackTrace();

				}

		}

	}

	/**
	 * @author Ryan Robinson Created on 4:13:49 PM Nov 3, 2011
	 * @param ip
	 * 
	 */
	public void makeClient(String ip) {
		try {
			peer = new Socket(ip, 4000);
			connected = true;
		} catch (UnknownHostException e) {

			System.out.println("IP Address Malformed");
			return;
		} catch (ConnectException ce) {
			System.out.println("Connection Refused");
			return;
		} catch (IOException e) {
			e.printStackTrace();

			// Clean up the socket if it fails to connect
			if (peer != null)
				try {
					peer.close();
				} catch (IOException ioe2) {
					ioe2.printStackTrace();
				}

		}

	}

	/**
	 * @return the givingList
	 * @author Chris Ray Created on 8:05:47 PM Oct 31, 2011
	 */
	public List<Item> getGivingList() {
		return givingList;
	}

	/**
	 * @return the receivingList
	 * @author Chris Ray Created on 8:05:52 PM Oct 31, 2011
	 */
	public List<Item> getReceivingList() {
		return receivingList;
	}

	/**
	 * @return the server
	 * @author Chris Ray Created on 8:05:58 PM Oct 31, 2011
	 */
	public ServerSocket getServer() {
		return server;
	}

	/**
	 * @return the peer
	 * @author Chris Ray Created on 8:06:08 PM Oct 31, 2011
	 */
	public Socket getPeer() {
		return peer;
	}

	/**
	 * @author Ryan Robinson Created on 4:13:49 PM Nov 3, 2011
	 * @return boolean corresponding to rather there exists a working connection
	 *         between the client and the server
	 * 
	 */
	public boolean getConnected() {
		return connected;
	}

	public List<Item> getOffer() throws StopTradeException{

		if (connected) {
			try {
				ObjectInputStream ois = new ObjectInputStream(
						peer.getInputStream());
				receivingList = (List<Item>) ois.readObject();
				System.out.println("got" + receivingList.toString());
				// ois.close();
			} catch (Exception e) {
				//e.printStackTrace();
				System.out
						.println("An Error occurred while receiving items..... \nThat may have been obvious.");
				receivingList = navigatorList;
				throw new StopTradeException();
			}
			return receivingList;
		}
		return null;

	}

	public void sendOffer() {

		if (connected)
			try {
				System.out.println("give "+ givingList.toString());
				ObjectOutputStream oos = new ObjectOutputStream(
						peer.getOutputStream());
				oos.writeObject(givingList);
				oos.flush();
				// oos.close();
			} catch (Exception e) {
				e.printStackTrace();
				System.out
						.println("An Error occurred while sending items..... \nThat may have been obvious.");

			}

	}

	public void sendConf(boolean b) {

		if (connected)
			try {
				ObjectOutputStream oos = new ObjectOutputStream(
						peer.getOutputStream());
				oos.writeBoolean(b);
				oos.flush();
				// oos.close();
			} catch (Exception e) {
				e.printStackTrace();
				System.out
						.println("An Error occurred while sending items..... \nThat may have been obvious.");
				receivingList = null;
			}

	}

	public boolean getConf() throws StopTradeException{
		boolean result = false;
		if (connected)
			try {
				ObjectInputStream ois = new ObjectInputStream(
						peer.getInputStream());
				result = ois.readBoolean();
				// ois.close();
			} catch (Exception e) {
				
				System.out
						.println("An Error occurred while receiving items..... \nThat may have been obvious.");
				throw new StopTradeException();
			}

		return result;

	}

	public void done() {
		try {
			if (server != null)
			server.close();
			if (peer != null)
			peer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("that didn't go well...");
		}

	}

}